Skip to content

feat(analytics): posthog audit — remove noise, add 10 new events#3960

Merged
waleedlatif1 merged 4 commits intostagingfrom
cherry/posthog-audit
Apr 4, 2026
Merged

feat(analytics): posthog audit — remove noise, add 10 new events#3960
waleedlatif1 merged 4 commits intostagingfrom
cherry/posthog-audit

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Re-land of #3917 — cherry-picked onto clean staging after accidental branch deletion.

Remove task_marked_read (fires automatically on every task view).

Add workspace_id to task_message_sent for group analytics.

New events:
- search_result_selected: block/tool/trigger/workflow/table/file/
  knowledge_base/workspace/task/page/docs with query_length
- workflow_imported: count + format (json/zip)
- workflow_exported: count + format (json/zip)
- folder_created / folder_deleted
- logs_filter_applied: status/workflow/folder/trigger/time
- knowledge_base_document_deleted
- scheduled_task_created / scheduled_task_deleted
@cursor
Copy link
Copy Markdown

cursor bot commented Apr 4, 2026

PR Summary

Low Risk
Low risk: changes are limited to emitting additional PostHog analytics events and removing one noisy event, with no functional business-logic changes beyond extra side-effect calls.

Overview
Adds new PostHog tracking across key user actions: folder create/delete, scheduled task create/delete, workflow import/export, logs filter application, search result selections, and knowledge base document deletion (including workspace group attribution where available).

Reduces analytics noise by removing the task_marked_read event and updating task_message_sent to always include workspace_id; the PostHog event catalog (events.ts) is expanded to type these new events/properties.

Reviewed by Cursor Bugbot for commit 8858f57. Configure here.

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 4, 2026 11:49pm

Request Review

@waleedlatif1 waleedlatif1 merged commit b4d9b8c into staging Apr 4, 2026
7 checks passed
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8858f57. Configure here.

onOpenChangeRef.current(false)
},
[workspaceId]
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Four identical handlers differ only by analytics string

Low Severity

handleTableSelect, handleFileSelect, and handleKbSelect are exact copies of handleTaskSelect, differing only in the result_type string. The same file already demonstrates a factory pattern via handleBlockSelect (which accepts a type parameter) with thin handleBlockSelectAsBlock/handleBlockSelectAsTool/handleBlockSelectAsTrigger wrappers. These four handlers could follow that same pattern with a single shared useCallback that takes a result_type parameter, reducing the risk of inconsistent future changes.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 8858f57. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 4, 2026

Greptile Summary

This PR is a PostHog analytics audit that removes noisy events and adds 13 new, targeted event captures across server-side API routes and client-side components. The changes span folder lifecycle events, scheduled task CRUD, knowledge-base document deletion, task/chat operations, log filter interactions, search result selection, and workflow import/export. The event catalog (events.ts) is updated as a single source of truth, and all new event types have corresponding capture calls verified in the codebase.

Key changes:

  • New server events: folder_created, folder_deleted, scheduled_task_created, scheduled_task_deleted, knowledge_base_document_deleted, task_renamed, task_deleted, task_marked_unread — all captured in their respective API routes.
  • New client events: task_message_sent, logs_filter_applied (5 filter types), search_result_selected (12 result types), workflow_imported, workflow_exported — captured using the posthogRef ref pattern with usePostHog.
  • Pattern consistency: All new client-side captures correctly use an inline posthogRef.current = posthog update inside useCallback closures — except home.tsx which uses a useEffect to update the ref instead, diverging from the pattern established throughout this PR.

Confidence Score: 5/5

Safe to merge — all new analytics captures are fire-and-forget and cannot affect functional behavior.

The sole finding is a P2 style inconsistency in home.tsx (useEffect vs. inline ref update). All event captures are correctly typed, guarded against missing session/workspace data, and consistent with the shared PostHogEventMap. No logic bugs, data-loss risks, or security issues identified.

apps/sim/app/workspace/[workspaceId]/home/home.tsx — minor posthogRef update pattern inconsistency.

Important Files Changed

Filename Overview
apps/sim/lib/posthog/events.ts Adds type definitions for 13 new analytics events; all verified to have corresponding capture calls in the codebase.
apps/sim/app/workspace/[workspaceId]/home/home.tsx Adds task_message_sent capture on submit; posthogRef update uses useEffect instead of the inline assignment pattern used in every other file in this PR.
apps/sim/app/api/schedules/[id]/route.ts Adds scheduled_task_deleted capture; fires even when workspaceId is null by falling back to empty string and omitting groups.
apps/sim/app/api/schedules/route.ts Adds scheduled_task_created capture on POST; straightforward and correct.
apps/sim/app/api/mothership/chats/[chatId]/route.ts Adds task_renamed, task_marked_unread, and task_deleted captures; all guarded by workspaceId presence.
apps/sim/app/api/knowledge/[id]/documents/[documentId]/route.ts Adds knowledge_base_document_deleted capture after audit record; nullable workspaceId handled safely.
apps/sim/app/api/folders/route.ts Adds folder_created capture after successful folder insertion with correct workspace group.
apps/sim/app/api/folders/[id]/route.ts Adds folder_deleted capture after successful deletion with correct workspace group.
apps/sim/app/workspace/[workspaceId]/logs/components/logs-toolbar/logs-toolbar.tsx Adds logs_filter_applied for all five filter types including custom date range; posthogRef updated inline correctly.
apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/search-modal/search-modal.tsx Adds search_result_selected capture across all 12 result-type handlers; posthogRef updated inline correctly.
apps/sim/app/workspace/[workspaceId]/w/hooks/use-export-workflow.ts Adds workflow_exported capture with correct format (json/zip) and count; posthogRef inline pattern used correctly.
apps/sim/app/workspace/[workspaceId]/w/hooks/use-import-workflow.ts Adds workflow_imported capture with correct format and count; only fires when at least one workflow succeeded.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    subgraph Server["Server-side API Routes"]
        A["POST /api/folders"] -->|folder_created| PH
        B["DELETE /api/folders/:id"] -->|folder_deleted| PH
        C["DELETE /api/knowledge/:id/documents/:docId"] -->|knowledge_base_document_deleted| PH
        D["POST /api/schedules"] -->|scheduled_task_created| PH
        E["DELETE /api/schedules/:id"] -->|scheduled_task_deleted| PH
        F["PATCH /api/mothership/chats/:chatId"] -->|task_renamed or task_marked_unread| PH
        G["DELETE /api/mothership/chats/:chatId"] -->|task_deleted| PH
    end
    subgraph Client["Client-side Components and Hooks"]
        H["home.tsx handleSubmit"] -->|task_message_sent| PH
        I["logs-toolbar.tsx filter handlers"] -->|logs_filter_applied| PH
        J["search-modal.tsx select handlers"] -->|search_result_selected| PH
        K["use-export-workflow.ts"] -->|workflow_exported| PH
        L["use-import-workflow.ts"] -->|workflow_imported| PH
    end
    PH["PostHog Analytics"]
Loading

Comments Outside Diff (1)

  1. apps/sim/app/workspace/[workspaceId]/home/home.tsx, line 35 (link)

    P2 Stale ref pattern — use inline assignment instead of useEffect

    Every other file in this PR (logs-toolbar.tsx, search-modal.tsx, use-export-workflow.ts, use-import-workflow.ts) updates posthogRef.current synchronously during render:

    const posthogRef = useRef(posthog)
    posthogRef.current = posthog   // inline — always fresh on every render

    Here, the ref is initialized on line 35 but only updated inside a useEffect (lines 221–223). This creates a brief window after a render where posthogRef.current could be stale if posthog changes. While PostHog's instance is practically stable after initialization, the approach is inconsistent with the rest of this PR. Add an inline assignment right after the ref declaration and remove the useEffect:

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Reviews (1): Last reviewed commit: "fix(analytics): correct format field log..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant